﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Auditors_CompanyAuditorsDelegate : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (int v in Enum.GetValues(typeof(AuditorsStatus)))
            {
                AuditorsStatus pStatus = (AuditorsStatus)v;
                DropDownList1.Items.Add(new ListItem(RemarkAttribute.GetEnumRemark(pStatus), v.ToString()));
            }
            DropDownList1.Items.Add(new ListItem("所有", "Status"));
            Bind("Status");
        }
    }

    private void Bind(string status)
    {
        var v = new OrderBLL().GetHongKongFileHelperOrdersForAuditors().Select("Status = " + status);
        if (v.Count() > 0)
        {
            if(!string.IsNullOrEmpty(TextBox1.Text))
            {
                DateTime dt = new DateTime();
                try
                {
                     dt = Convert.ToDateTime(TextBox1.Text + "-01");
                }
                catch (Exception)
                {
                    AlertMsg("时间格式错误!");
                    return;
                }
                v = v.CopyToDataTable().Select(string.Format("下单时间 >= '{0}-1' and 下单时间 < '{1}-1'", dt.ToString("yyyy-MM"), dt.AddMonths(1).ToString("yyyy-MM")));
            }
            if (v.Count() > 0)
            {
                GridView1.DataSource = v.CopyToDataTable();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }

    }

    /// <summary>
    /// 行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton Signature = e.Row.FindControl("Signature") as LinkButton;
            LinkButton QuoteRevenue = e.Row.FindControl("QuoteRevenue") as LinkButton;
            LinkButton Finish = e.Row.FindControl("Finish") as LinkButton;

            int status = Convert.ToInt32(e.Row.Cells[7].Text);
            AuditorsStatus pStatus = (AuditorsStatus)status;
            e.Row.Cells[7].Text = RemarkAttribute.GetEnumRemark(pStatus);


            switch (pStatus)
            {
                case AuditorsStatus.Assigned:
                    Signature.Visible = true;
                    break;
                case AuditorsStatus.Signature:
                    QuoteRevenue.Visible = true;
                    break;
                case AuditorsStatus.QuoteRevenue:
                    Finish.Visible = true;
                    break;
            }
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();

        switch (e.CommandName)
        {
            case "Signature":
                new CompanyBLL().UpdateCompanyAuditorsStatuc(id, (int)AuditorsStatus.Signature, Page.User.Identity.Name);
                break;
            case "QuoteRevenue":
                new CompanyBLL().UpdateCompanyAuditorsStatuc(id, (int)AuditorsStatus.QuoteRevenue, Page.User.Identity.Name);
                break;
            case "Finish":
                new CompanyBLL().UpdateCompanyAuditorsStatuc(id, (int)AuditorsStatus.Finish, Page.User.Identity.Name);
                break;
        }
        AlertMsg("操作成功");
        Bind(DropDownList1.SelectedValue);
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Bind(DropDownList1.SelectedValue);
    }
}